using System;
using System.Collections;

namespace ScenarioFramework.WorkflowEngine.DataModel.Common
{
	//*********************************************************************
	//
	// XMLFormalParameters Class
	//
	// The XMLFormalParameters class represents a collection base of XMLFormalParameter
	// Its imports methods from WAPI.XmlObjectSet like get set and add
	// XMLFormalParameters Has no fields
	//
	//*********************************************************************

	public class XMLFormalParameters : bOSdotNET.XML.Data.XMLObjectSet 
	{
		public const string MAIN_NODE		= "Parameters";

		/** FIELDS **/

		/** no fields **/

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Empty **/
		public XMLFormalParameters()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLFormalParameters(System.Xml.XmlNode nodeList)
		{
			this.Parse(nodeList);
		}

		/** INSTANCE METHODS **/

		public override void Init()
		{
			this.RemoveAll();
		}

		/**Method to parse an xml string into a nodelist collection **/

		override public void Parse(System.Xml.XmlNode nodeList)
		{
			this.Init();

			System.Xml.XmlNode node;

			for ( int i=0; i<nodeList.ChildNodes.Count;i++)
			{
				node = nodeList.ChildNodes[i];

				switch ( node.Name) 
				{
					case XMLFormalParameter.MAIN_NODE:
						this.Add ( new XMLFormalParameter(node));
						break;
				}
			}
		}

		/** Method for return a functionParameter from collection **/
		public XMLFormalParameter Get(string sName)
		{
			XMLFormalParameter functionParameter;

			for ( int i=0; i<this.List.Count; i++)
			{
				functionParameter = (XMLFormalParameter)this.List[i];
				if ( functionParameter.Name == sName )
				{
					return functionParameter;
				}
			}

			throw new ScenarioFramework.WorkflowEngine.DataModel.Exception.FormalParameterNotFound(sName);
		}

		/**Method to retive a nodelist from collection **/
		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLFormalParameters.MAIN_NODE);

			for ( int i=0; i<this.Count;i++)
			{
				xmlNode.AddNode ( this[i].GetXMLNode() );

			}

			return xmlNode;
		}

		/** SORTING  FIELDS **/

		public enum SortFields 
		{
			Name,
			Type,
			Mandatory,
			Value
		}

		/** SORTING METHODS **/


		public void Sort(SortFields sortField, bool isAscending) 
		{
			switch (sortField) 
			{
				case SortFields.Name:
					InnerList.Sort(new NameComparer());
					break;
				case SortFields.Type:
					InnerList.Sort(new TypeComparer());
					break;
				case SortFields.Mandatory:
					InnerList.Sort(new MandatoryComparer());
					break;
				case SortFields.Value:
					InnerList.Sort(new ValueComparer());
					break;
			}
			if (!isAscending) InnerList.Reverse();
		}


		private sealed class NameComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLFormalParameter first = (XMLFormalParameter) x;
				XMLFormalParameter second = (XMLFormalParameter) y;
				return first.Name.CompareTo(second.Name);
			}
		}

		private sealed class TypeComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLFormalParameter first = (XMLFormalParameter) x;
				XMLFormalParameter second = (XMLFormalParameter) y;
				return first.Type.CompareTo(second.Type);
			}
		}

		private sealed class MandatoryComparer : IComparer 
		 {
			 public int Compare(object x, object y) 
			 {
				 XMLFormalParameter first = (XMLFormalParameter) x;
				 XMLFormalParameter second = (XMLFormalParameter) y;
				 return first.Mandatory.CompareTo(second.Mandatory);
			 }
		 }

		private sealed class ValueComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLFormalParameter first = (XMLFormalParameter) x;
				XMLFormalParameter second = (XMLFormalParameter) y;
				return first.Value.CompareTo(second.Value);
			}
		}
	}
}